package com.are.odisea;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.are.entidades.PreguntasEncuestas;
import com.are.manejadores.ManejadorPregEnc;

/**
 * Servlet implementation class SrvPermisosRolEnc
 */
@WebServlet("/SrvPermisosRolEnc")
public class SrvPermisosRolEnc extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SrvPermisosRolEnc() {
        super();
        // TODO Auto-generated constructor stub
    }
    db conexion = null;
    ManejadorPermisosRolEnc manejador;
    
    protected void ProcesarPeticion(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
    	response.setContentType("text/html;charset=ISO-8859-1");
    	PrintWriter out = response.getWriter();

		String operacion = (String) request.getParameter("operacion");
		
	if(operacion.equals("add")){
			RegistrarRol(request, out);
	}
	else if(operacion.equals("list")){
			Listar(request, out);
	}
	else if(operacion.equals("remove")){
		Remove(request, out);
}
	out.close();
    }
    
    protected void Remove(HttpServletRequest request,PrintWriter out){
    	int keyR = Integer.parseInt((String) request.getParameter("keyR"));
    	int keyE = Integer.parseInt((String) request.getParameter("keyE"));
		try {
			conexion = new db();
			manejador = new ManejadorPermisosRolEnc(conexion);
			if (manejador.exist(keyE,keyR)) {
				if (manejador.remove(keyE,keyR)) {
					out.print("OK");
				}else {
					out.print("Error al procesar la solicitud");
				}
				
			}else {
				out.print("El Permiso: " + keyR+keyE+" no existe.");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			out.print("Error de conexion con el servidor: "
					+ e.getMessage());
		} finally {
			if (conexion != null) {
				try {
					conexion.Close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					out.print(e.getMessage());
				}
			}
		}

    }
    
    protected void RegistrarRol(HttpServletRequest request,PrintWriter out){
    	try {
    		String roles = (String) request.getParameter("roles");
    		String [] rol=roles.split(",");
        	int j=0;
        	conexion = new db();
    		while(j<rol.length){
    			PermisosRolEnc enc = new PermisosRolEnc();
    			enc.setRol(Integer.parseInt(rol[j]));
    			enc.setId_enc(Integer.parseInt(request.getParameter("id_enc")));
    			manejador=new ManejadorPermisosRolEnc(conexion);
    			manejador.setPre(enc);
    			if (manejador.add()) {
    				out.print("OK");
    			}else {
    				out.print("Error al procesar la solicitud");
    			}
    			j++;
    		}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (conexion != null) {
				try {
					conexion.Close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					out.print(e.getMessage());
				}//fin del cath
			}//fin del if dentro de
		}
    }
    
    protected void Listar(HttpServletRequest request,PrintWriter out){
    	int id_enc=Integer.parseInt(request.getParameter("id_enc"));
		try {
			conexion = new db();
			manejador = new ManejadorPermisosRolEnc(conexion);
			ArrayList<PermisosRolEnc> enc = manejador.list(id_enc);

				out.println("<h2>Listado de Preguntas</h2>");
				out.println("<table>");	
				out.println("<thead>");
				out.println("<tr>");
				out.println("<th></th>");
				out.println("<th>Rol</th>");
				out.println("<th></th>");
				out.println("</tr>");
				out.println("</thead>");
				out.println("<tbody>");
				int fila=1;
				for (PermisosRolEnc e : enc)
				{
					out.println("<tr " + (fila%2==0?"class='odd'":"") + ">");
					out.println("<td>" + fila + "</td>");
					out.println("<td>" + e.getDesRol()+ "</td>");
					out.println("<td>");
					out.print("<a href=\"javascript:remove('" 
							+ e.getRol()+"','"+e.getId_enc() 
							+ "')\">Eliminar</a>");
					out.println("</td>");
					out.println("</tr>");
					fila++;
				} 
				out.println("<tr>");
				out.println("</tr>");
				out.println("<tbody>");
				out.println("<tfoot>");
				out.println("<th colspan='3'>Total Usuarios: " + enc.size() +"</th>");
				out.println("</tfoot>");
				out.println("</table>");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			out.print(e.getMessage());
		} finally {
			if (conexion != null) {
				try {
					conexion.Close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					out.print(e.getMessage());
				}
			}
		}
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ProcesarPeticion(request,
				response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ProcesarPeticion(request,
				response);
	}

}
